/*
	Title: 02_Table12.do
	Paper: Brauning, Ivashina, Ozdagli (2025)
	Description: Table 12 - Debt response to covenant trigger
*/

/*------------------------------------------------------------------------------
								  Table 12
------------------------------------------------------------------------------*/
// Import Table 12 Debt LHS data -----------------------------------------------
use "$data_path/Table12_Debt.dta", clear 


// Table Setup -----------------------------------------------------------------
putdocx clear 
putdocx begin
putdocx paragraph
putdocx table table12 = (20, 7), border(insideV, nil) border(insideH, nil) border(left, nil) border(right, nil)

* Top margin
putdocx table table12(1,2), colspan(3)
putdocx table table12(1,2) = ("Δ Debt/Asset (ppt)")
putdocx table table12(1,3), colspan(3)
putdocx table table12(1,3) = ("Δ Leverage Ratio")

putdocx table table12(2,4) = ("Both types")
putdocx table table12(2,7) = ("Both types")
putdocx table table12(2,.), border(bottom)

putdocx table table12(3,2) = ("(1)")
putdocx table table12(3,3) = ("(2)")
putdocx table table12(3,4) = ("(3)")
putdocx table table12(3,5) = ("(4)")
putdocx table table12(3,6) = ("(5)")
putdocx table table12(3,7) = ("(6)")
putdocx table table12(3,.), border(bottom)

* Left margin
putdocx table table12(5,1) = ("Bind")
putdocx table table12(7,1) = ("Incurrence bind")
putdocx table table12(9,1) = ("Maintenance bind")
putdocx table table12(12,1) = ("Observations")
putdocx table table12(13,1) = ("R-squared")
putdocx table table12(14,1) = ("Firm FE")
putdocx table table12(15,1) = ("Quarter FE")
putdocx table table12(16,1) = ("Controls")
putdocx table table12(17,1) = ("Controls*Cov-type")
putdocx table table12(18,1) = ("Quarter FE*Cov-type")
putdocx table table12(19,1) = ("H0: Incurrence=Maintenance")
putdocx table table12(20,1) = ("p-value")


// Define Regression Specifications --------------------------------------------
local LHS "D_debt_ta_tr"

local controls0   "L_macroq_tr L_logatq_tr cashflowq_tr" 
local dd_controls "L_DD_Total_tr L_DD_Total_sq_tr"
local controls    "`controls0' `dd_controls'"

local cond0 "L.dealscan <= 1"
local cond1 "_b1_diff_std <= 2 & _b2_diff_std <= 0.5"
local cond2 "(!mi(lev_limit) | !mi(intcov_limit) | !mi(Ilev_limit) | !mi(Iintcov_limit))"
local conditions "`cond0' & `cond1' & `cond2'"
	
	
// Column (1) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' c.(L_in_covenant_violation) `controls' L_incurrence L_maintenance if `conditions', ///
			  absorb(xdim qdate) cluster(xdim qdate)

// Store results 
matrix reg_results = r(table)
local violation_b : display %04.2f reg_results["b", "L_in_covenant_violation"]  
local violation_se : display %04.2f reg_results["se", "L_in_covenant_violation"]
local violation_p = reg_results["pvalue", "L_in_covenant_violation"]
if (`violation_p' > 0.1) {
	local violation_sigstars = ""
} 
if (`violation_p' < 0.1) {
	local violation_sigstars = "*"
}
if (`violation_p' < 0.05) {
	local violation_sigstars = "**"
}
if (`violation_p' < 0.01) {
	local violation_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f e(r2)

// Put results into table in Word Document 
putdocx table table12(5,2) = ("`violation_b'`violation_sigstars'")
putdocx table table12(6,2) = ("(`violation_se')")
putdocx table table12(12,2) = (`N')
putdocx table table12(13,2) = (`r2')
putdocx table table12(14,2) = ("Yes")
putdocx table table12(15,2) = ("Yes")
putdocx table table12(16,2) = ("Yes")
putdocx table table12(17,2) = ("No")
putdocx table table12(18,2) = ("No")


// Column (2) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions', ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)
	
// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]  
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.3f  r(p)

// Put results into table in Word Document
putdocx table table12(7,3) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table12(8,3) = ("(`incurrence_se')")
putdocx table table12(9,3) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table12(10,3) = ("(`maintenance_se')")
putdocx table table12(12,3) = (`N')
putdocx table table12(13,3) = (`r2')
putdocx table table12(14,3) = ("Yes")
putdocx table table12(15,3) = ("Yes")
putdocx table table12(16,3) = ("Yes")
putdocx table table12(17,3) = ("Yes")
putdocx table table12(18,3) = ("Yes")
putdocx table table12(19,3) = (`estimate')
putdocx table table12(20,3) = (`p')

	
// Column (3) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & incurrence == 1 & maintenance == 1, ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate) 
	
// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.3f r(p)

// Put results into table in Word Document
putdocx table table12(7,4) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table12(8,4) = ("(`incurrence_se')")
putdocx table table12(9,4) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table12(10,4) = ("(`maintenance_se')")
putdocx table table12(12,4) = (`N')
putdocx table table12(13,4) = (`r2')
putdocx table table12(14,4) = ("Yes")
putdocx table table12(15,4) = ("Yes")
putdocx table table12(16,4) = ("Yes")
putdocx table table12(17,4) = ("Yes")
putdocx table table12(18,4) = ("Yes")
putdocx table table12(19,4) = (`estimate')
putdocx table table12(20,4) = (`p')
	
	
// Import Table 12 Leverage Ratio LHS data -------------------------------------
use "$data_path/Table12_LR.dta", clear 


// Define Regression Specifications --------------------------------------------
local LHS "D_leverage_ratio_tr"

local controls0   "L_macroq_tr L_logatq_tr cashflowq_tr" 
local dd_controls "L_DD_Total_tr L_DD_Total_sq_tr"
local controls    "`controls0' `dd_controls'"

local cond0 "L.dealscan <= 1"
local cond1 "_b1_diff_std <= 2 & _b2_diff_std<= 0.5"
local cond2 "(!mi(lev_limit) | !mi(intcov_limit) | !mi(Ilev_limit) | !mi(Iintcov_limit))"
local conditions "`cond0' & `cond1' & `cond2'"
	
	
// Column (4) ------------------------------------------------------------------
// Regression: 	
reghdfe `LHS' c.(L_in_covenant_violation) `controls' L_incurrence L_maintenance if `conditions', ///
			  absorb(xdim qdate) cluster(xdim qdate)
			  
// Store results 
matrix reg_results = r(table)
local violation_b : display %04.2f reg_results["b", "L_in_covenant_violation"]   
local violation_se : display %04.2f reg_results["se", "L_in_covenant_violation"]
local violation_p = reg_results["pvalue", "L_in_covenant_violation"]
if (`violation_p' > 0.1) {
	local violation_sigstars = ""
} 
if (`violation_p' < 0.1) {
	local violation_sigstars = "*"
}
if (`violation_p' < 0.05) {
	local violation_sigstars = "**"
}
if (`violation_p' < 0.01) {
	local violation_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f e(r2)

// Put results into table in Word Document
putdocx table table12(5,5) = ("`violation_b'`violation_sigstars'")
putdocx table table12(6,5) = ("(`violation_se')")
putdocx table table12(12,5) = (`N')
putdocx table table12(13,5) = (`r2')
putdocx table table12(14,5) = ("Yes")
putdocx table table12(15,5) = ("Yes")
putdocx table table12(16,5) = ("Yes")
putdocx table table12(17,5) = ("No")
putdocx table table12(18,5) = ("No")

	
// Column (5) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions', ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)
	
// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]   
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.3f  r(p)

// Put results into table in Word Document
putdocx table table12(7,6) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table12(8,6) = ("(`incurrence_se')")
putdocx table table12(9,6) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table12(10,6) = ("(`maintenance_se')")
putdocx table table12(12,6) = (`N')
putdocx table table12(13,6) = (`r2')
putdocx table table12(14,6) = ("Yes")
putdocx table table12(15,6) = ("Yes")
putdocx table table12(16,6) = ("Yes")
putdocx table table12(17,6) = ("Yes")
putdocx table table12(18,6) = ("Yes")
putdocx table table12(19,6) = (`estimate')
putdocx table table12(20,6) = (`p')
	
	
// Column (6) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & incurrence == 1 & maintenance == 1, ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate) 
			  
// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]   
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 : display %9.2f e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p: display %9.3f r(p)

// Put results into table in Word Document
putdocx table table12(7,7) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table12(8,7) = ("(`incurrence_se')")
putdocx table table12(9,7) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table12(10,7) = ("(`maintenance_se')")
putdocx table table12(12,7) = (`N')
putdocx table table12(13,7) = (`r2')
putdocx table table12(14,7) = ("Yes")
putdocx table table12(15,7) = ("Yes")
putdocx table table12(16,7) = ("Yes")
putdocx table table12(17,7) = ("Yes")
putdocx table table12(18,7) = ("Yes")
putdocx table table12(19,7) = (`estimate')
putdocx table table12(20,7) = (`p')


// Table Setup (end) -----------------------------------------------------------
putdocx table table12(.,2/7), halign(center)
putdocx table table12(.,1), width(3 in)

* Number formatting
putdocx table table12(6,.), nformat(%04.2f)
putdocx table table12(8,.), nformat(%04.2f)
putdocx table table12(10,.), nformat(%04.2f)
putdocx table table12(12,.), nformat(%9.0fc)
putdocx table table12(13,.), nformat(%9.2g)
putdocx table table12(19,.), nformat(%05.3f)
putdocx table table12(20,.), nformat(%9.3g)

* Table formatting
putdocx table table12(.,.), font("Times New Roman", 9)

// Export Table 4
putdocx save "$tables_path/Table12.docx", replace	